package com.netty.sever;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;

/**
 * @description:
 * @date 2020-04-01 13:57
 * @author: liujinhu
 */
public class HelloNettyServerInitializer extends ChannelInitializer<SocketChannel> {
    //对chanel进行初始化
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //通过socketChannel去获得对应的管道
        ChannelPipeline channelPipeline = socketChannel.pipeline();

//        channelPipeline.addLast(new LineBasedFrameDecoder(1024));
        /**
         * pipeline中会有很多handler类（也称之拦截器类）
         * 获得pipeline之后，可以直接.add，添加不管是自己开发的handler还是netty提供的handler
         *
         */
        //一般来讲添加到last即可，添加了一个handler并且取名为HttpServerCodec
        //当请求到达服务端，要做解码，响应到客户端做编码

        channelPipeline.addLast(new StringDecoder());// 接收到字符串的解码器。
        channelPipeline.addLast(new StringEncoder());// 发送字符串的编码器。
        channelPipeline.addLast(new CustomHandler());
//        channelPipeline.addLast("HttpServerCodec", new HttpServerCodec());
        //添加自定义的CustomHandler这个handler，返回Hello Netty
//        channelPipeline.addLast("customHandler", new CustomHandler());

        System.out.println("SimpleChatClient:" + socketChannel.remoteAddress()+"连接上服务器");


    }

}
